package com.czm.art_light.config;

import com.czm.art_light.constant.ApiRouterConsts;
import com.czm.art_light.interceptor.AuthInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Spring Web Mvc 相关配置不要加 @EnableWebMvc 注解，否则会导致 jackson 的全局配置失效。因为 @EnableWebMvc 注解会导致 WebMvcAutoConfiguration 自动配置失效
 */
@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

    private final AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 权限认证拦截
        registry.addInterceptor(authInterceptor)
                // 拦截请求接口
                .addPathPatterns(ApiRouterConsts.API_URL_PREFIX + "/**")
                // 放行登录相关请求接口
                .excludePathPatterns(ApiRouterConsts.USER_API_URL_PREFIX + "/login")
                .order(2);

    }

}
